<template>
    <div class="wrapper">
        <!-- <v-header /> -->
        <v-sidebar />
        <div data-tauri-drag-region class="content-box" :class="{ 'content-collapse': sidebar.collapse }">
        <div data-tauri-drag-region class="header-right">
            <div class="platform-name" data-tauri-drag-region>
            <img class="logo-tag" data-tauri-drag-region :src="systemIcon" alt="" />
                <span data-tauri-drag-region>{{systemName}}</span>
                <span v-if="unit_name" class="divider-title">|</span>
                <span data-tauri-drag-region v-if="unit_name" class="unit-font">  {{unit_name}}</span>
                </div>
            <div class="window-btn">
                <el-icon size="16" @click="hideScreen" class="icon-font"><Minus  /></el-icon>
                <el-icon size="16" @click="setFullScreen" class="icon-font icon-btn"><FullScreen  /></el-icon>
                <el-icon size="16" @click="closeScreen" class="icon-font"><CloseBold  /></el-icon>
            </div>
        </div>
            <!-- <v-tabs></v-tabs> -->
            <div class="content">
                <router-view v-slot="{ Component }">
                    <transition name="move" mode="out-in">
                        <keep-alive :include="tabs.nameList">
                            <component :is="Component"></component>
                        </keep-alive>
                    </transition>
                </router-view>
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">
import { ref, watch, onMounted } from 'vue';
import { useSidebarStore } from '@/store/sidebar';
import { useTabsStore } from '@/store/tabs';
import { Window } from '@tauri-apps/api/window';
// import vHeader from '@/components/header.vue';
import vSidebar from '@/components/sidebar.vue';
// import vTabs from '@/components/tabs.vue';
import { useSystemStore } from '@/store/system';
import basisIcon from '@/assets/img/logo1.png';
import  siteConfigService  from '@/api/service/siteConfigService'
import { getFileStream } from '@/api/index'

const sidebar = useSidebarStore();
const tabs = useTabsStore();
const window = Window.getCurrent();
const system = useSystemStore()
const systemName = ref()
systemName.value = localStorage.getItem('systemName') || '视听整合连续性训练系统'

watch(()=> system.systemName, (newValue, oldValue) => {
    systemName.value = newValue
})
watch(()=> system.systemIcon, (newValue, oldValue) => {
    systemIcon.value = newValue
})


const systemIcon = ref()
systemIcon.value = basisIcon
// 加载数据
const getData = async () => {
  try {
    const res = await siteConfigService.getById(1)
    if (res && Object.keys(res).length > 0) {
      // 如果logo字段有值，加载图片
      if (res.logo) {
        try {
          const file = await getFileStream(res.logo);
          systemIcon.value = URL.createObjectURL(new Blob([file.buffer])) || basisIcon
        } catch (error) {
          console.warn('无法加载logo图片:', error);
        }
      }
    }
  } catch (error) {
    console.error('加载配置数据失败:', error);
  }
}

onMounted(() => {
  getData();
});

const unit_name = ref()
unit_name.value = localStorage.getItem('unit_name') || ''
watch(()=> system.unit_name, (newValue, oldValue) => {
    unit_name.value = newValue
})

const setFullScreen = async() => {
    await window.toggleMaximize();
};
const hideScreen = async () => {
    await window.minimize();
}
const closeScreen = async () => {
    await window.close();
}
</script>

<style scoped>
.logo-tag {
    width: 25px;
    height: 25px;
    padding-right: 10px;
}

.divider-title {
    margin: 0 10px;
}

.unit-font {
    font-size: 14px;
    color: #409EFF;
    background: transparent;
}
</style>
<style>
.wrapper {
    height: 100vh;
    overflow: hidden;
}
.content-box {
    position: absolute;
    left: var(--sidebar-width-default);
    right: 0;
    top: 0px;
    bottom: 0;
    padding-bottom: 30px;
    -webkit-transition: left 0.3s ease-in-out;
    transition: left 0.3s ease-in-out;
    background: #eef0fc;
    overflow: hidden;
    padding-top: 10px;
}

.content {
    width: auto;
    height: 100%;
    padding: 20px;
    overflow-y: scroll;
    box-sizing: border-box;
    background-color: #fff;
}

.content::-webkit-scrollbar {
    width: 0;
}

.content-collapse {
    left: 65px;
}

.header-right {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 20px;
    padding-left: 10px;
    height: 40px;
}

.window-btn {
    margin-top: -10px;
}

.platform-name {
    font-size: 20px;
    font-weight: 600;
    color:#1C9F96;
    text-shadow:1px 1px #BAE6EF,2px 2px #BAE6EF,3px 3px #BAE6EF,4px 4px #BAE6EF,5px 5px #BAE6EF;
    background-color:#eef0fc;
    display: flex;
    user-select: none;
}

.icon-font {
    cursor: pointer;
}

.icon-btn {
    margin: 0 18px;
}
</style>
